//
//  YBCloudComputerDetailCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/8.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBCloudComputerDetailCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = .lightWhiteDark33
        contentView.backgroundColor = .lightWhiteDark33
        
        stackView.addArrangedSubviews([deviceNameItem, deviceConfigItem, deviceGraphicsItem, surplusTotalItem])
        contentView.addSubview(stackView)
        
        stackView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(13)
            make.left.right.equalToSuperview().inset(15)
            make.height.equalTo(20*4+8*3)
        }
        
    }
    
    /// 重写frame
    override var frame: CGRect {
        get { return super.frame }
        set {
            var frame = newValue
            frame.origin.x += 10
            frame.size.width -= 2*10
            super.frame = frame
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var stackView: UIStackView = {
        let _stackView = UIStackView()
        _stackView.axis = .vertical
        _stackView.spacing = 8
        _stackView.alignment = .fill
        _stackView.distribution = .fillEqually
        return _stackView
    }()
    
    private lazy var deviceNameItem = YBCloudComputerDetailItemView(type: .deviceName)
    private lazy var deviceConfigItem = YBCloudComputerDetailItemView(type: .deviceConfig)
    private lazy var deviceGraphicsItem = YBCloudComputerDetailItemView(type: .deviceGraphics)
    private lazy var surplusTotalItem = YBCloudComputerDetailItemView(type: .surplusTotal)
    
}

// MARK: - Public
extension YBCloudComputerDetailCell {
    
    func updateCell(by model: YBOrderInfoVosModel) {
        deviceNameItem.desLabel.text = model.type == 6 ? "大型游戏" : "休闲游戏"
        surplusTotalItem.desLabel.text = model.surplusTotal
    }
    
}

// MARK: - YBCloudComputerDetailItemView
class YBCloudComputerDetailItemView: UIView {
    
    enum ItemType: String {
        case deviceName = "设备名称："
        case deviceConfig = "设备配置："
        case deviceGraphics = "设备显卡："
        case surplusTotal = "剩余可用时间："
    }
    
    init(type: ItemType) {
        super.init(frame: .zero)
        
        titleLabel.text = type.rawValue
        
        self.backgroundColor = .clear
        self.addSubviews([roundView, titleLabel, desLabel])
        
        roundView.snp.makeConstraints { make in
            make.left.equalTo(5)
            make.size.equalTo(6)
            make.centerY.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(roundView.snp.right).offset(4)
            make.height.equalTo(20)
        }
        
        desLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(1)
            make.centerY.right.equalToSuperview()
            make.height.equalTo(20)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var roundView: UIImageView = {
        let image = UIImage.bk_gradient(size: CGSizeMake(6, 6), colors: [kRGBColor(88, 182, 255), kRGBColor(40, 128, 237)])
        let iv = UIImageView(image: image)
        iv.bk_addCornerBorder(radius: 3, borderWidth: 0.1, borderColor: .clear)
        return iv
    }()
    
    private lazy var titleLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    lazy var desLabel: UILabel = {
        let label = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray102)
        label.bk_setTruncatingTail()
        return label
    }()
    
}
